define(['./controllers'], function (controllers) {
    'use strict';
    controllers.controller('loginCtrl', ['$scope', '$http', '$state','apiUrl','$rootScope','$cookieStore','$modal','$stateParams','$location','messageServices',
    function($scope, $http, $state,apiUrl,$rootScope,$cookieStore,$modal,$stateParams,$location,messageServices) {
        console.log($stateParams.returl);
        $scope.user = {tel:"137",pw:"1"};
        $scope.authError = null;
        $scope.login = function() {
            $scope.authError = null;
            // Try to login
            $http.post(apiUrl+'/api/v1/jwtoken', {tel: $scope.user.tel, pw: $scope.user.pw})
                .then(function(response) {
                    if ( !response.data ) {
                        messageServices.openMessage('账户密码错误');
                    }else{
                        $rootScope.user = response.data;
                        $cookieStore.put("wq_use", $rootScope.user);
                        $rootScope.token = $rootScope.user.token;
                        if($stateParams.returl==""){
                            $state.go('home.list');
                            return;
                        }else{
                            $location.path($stateParams.returl);
                        }

                    }
                }, function(x) {
                    $scope.authError = 'Server Error';
                });
        };
    }])
});